package es.deusto.ingenieria.is.search.touringproblem.formulation;


import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import es.deusto.ingenieria.is.search.formulation.State;
import es.deusto.ingenieria.is.search.xml.StateXMLReader;



	/**
	 * @author irene.gallardo@opendeusto.es y daphne.aberasturi@opendeusto.es
	 * 			Lee los elementos del archivo xml
	 *
	 */
	public class MapReader extends StateXMLReader {

		private int cityLocationX;
		private int cityLocationY;
		private String cityName;
		private String type;
		//lista de ciudades
		private List<City> distancias;
		
		
		public MapReader(String xmlFile) {
			super(xmlFile);
		}
		
		public State getState() {
			return new EnvironmentMap(this.distancias);
		}
		//crea el mapa inicial, intruduce la ciudad leida en la lista 
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			
			try {		
				if (qName.equals("is:map")) {
					distancias= new ArrayList<City>();
				} else if (qName.equals("is:distribution")){
				} else if (qName.equals("is:start")){
					this.cityName = attributes.getValue("name").toUpperCase();
					this.cityLocationX= (int)Integer.parseInt(attributes.getValue("x"));;
					this.cityLocationY= (int)Integer.parseInt(attributes.getValue("y"));
					this.type="start";
					City ciudad= new City(cityLocationX, cityLocationY, cityName, type);
					this.distancias.add(ciudad);
				}
				else if(qName.equals("is:end"))
						{
					this.cityName = attributes.getValue("name").toUpperCase();
					this.cityLocationX= (int)Integer.parseInt(attributes.getValue("x"));
					this.cityLocationY= (int)Integer.parseInt(attributes.getValue("y"));
					this.type="end";
					City ciudad= new City(cityLocationX, cityLocationY, cityName, type);
					this.distancias.add(ciudad);
						}
				else if (qName.equals("is:city"))
				{
					this.cityName = attributes.getValue("name").toUpperCase();
					this.cityLocationX= (int)Integer.parseInt(attributes.getValue("x"));;
					this.cityLocationY= (int)Integer.parseInt(attributes.getValue("y"));
					this.type="normal";
					City ciudad= new City(cityLocationX, cityLocationY, cityName, type);
					this.distancias.add(ciudad);
				}
					
					
				
			} catch (Exception ex) {
				System.out.println(this.getClass().getName() + ".startElement(): " + ex);
			}
		}
	}